package com.smzd.framework.sso;

import java.util.List;

import weibo4j.Oauth;
import weibo4j.Users;
import weibo4j.http.AccessToken;

import com.smzd.framework.sso.domain.SocialUser;
import com.smzd.framework.system.domain.UserBind;

public class SinaWeiboOAuth2AuthenticationProcessor extends OAuth2AuthenticationProcessor {

    private final static String state = "sinaweibo";

    public SinaWeiboOAuth2AuthenticationProcessor() {
        super(state);
        this.platform = UserBind.AUTH_TYPE_SINA_WEIBO;
    }

    @Override
    public Object getAccessToken(String queryString) throws Exception {
        String qs[] = queryString.split("&");
        String code = null;

        for (int i = 0; i < qs.length; i++) {
            if (qs[i].startsWith("code")) {
                code = qs[i].split("=")[1];
                break;
            }
        }

        Oauth oauth = new Oauth();
        AccessToken accessTokenApi = oauth.getAccessTokenByCode(code);
        System.out.println("accessTokenApi=" + accessTokenApi);
        System.out.println("accessTokenApi.access_token=" + accessTokenApi.getAccessToken());
        System.out.println("accessTokenApi.expire_in=" + accessTokenApi.getExpireIn());

        return accessTokenApi;
    }

    @Override
    public Object getRemoteUserInfo(Object accessToken) throws Exception {
        AccessToken accessTokenApi = (AccessToken) accessToken;
        String access_token = accessTokenApi.getAccessToken();
        System.out.println("access_token=" + access_token);

        String uid = accessTokenApi.getUserUid();
        System.out.println("uId=" + uid);

        Users users = new Users();
        users.client.setToken(access_token);
        weibo4j.model.User snuser = users.showUserById(uid);
        // String userDomain = snuser.getUserDomain(); // 用户登录名
        String screenName = snuser.getScreenName(); // 用户呢称
        String avatar = snuser.getAvatarLarge(); // 用户头像
        String gender = snuser.getGender(); // 性别
        String description = snuser.getDescription(); // 用户描述

        SocialUser user = new SocialUser();
        user.setAccessToken(access_token);
        user.setPlatform(this.getPlatform());
        user.setName(screenName);
        user.setPassword(uid);
        user.setOpenid(uid);
        if (gender.equals("m")) {// 男
            user.setGender(0);
        } else {
            if (gender.equals("f")) {// 女
                user.setGender(1);
            } else {// 未知
                user.setGender(2);
            }
        }
        user.setAvatar(avatar);
        user.setDescription(description);

        return user;
    }

    @Override
    public SocialUser convertRemoteToSocialUser(Object remoteUserInfo) throws Exception {
        SocialUser remoteUser = (SocialUser) remoteUserInfo;
        return remoteUser;
    }

    @Override
    public String getAuthenticationURL() throws Exception {
        weibo4j.Oauth oauth = new weibo4j.Oauth();
        String url = oauth.authorize("code", "sinaweibo");
        return url;
    }

    @Override
    public List<String> getFriendOpenids(SocialUser socialUser, Object accessToken) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
